/*
 *	Copyright (C) 2008 - Mauricio Bieze Stefani
 *	This file is part of the Insano Game Engine.
 *
 *	Insano is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Insano is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Insano.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "GameUI.h"
#include "MBSTrace.h"

using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

std::string wc2str(const wchar_t *wc)
{
	char tmp[MAX_TEXT_SIZE+1];
	wcstombs(tmp, wc, MAX_TEXT_SIZE);
	return std::string(tmp);
}

int wc2int(const wchar_t *wc)
{
	char tmp[MAX_TEXT_SIZE+1];
	wcstombs(tmp, wc, MAX_TEXT_SIZE);
	return atoi(tmp);
}

void GUIManager::setText(int id, const char* txt, ...)
{
	wchar_t temp[MAX_TEXT_SIZE+1];
	wchar_t wtemp[MAX_TEXT_SIZE+1];
	irr::gui::IGUIElement *e=getElement(id);
	if(txt)
	{
		mbstowcs(temp, txt, MAX_TEXT_SIZE);

		va_list val;
		va_start( val, txt );
		vswprintf(wtemp, MAX_TEXT_SIZE, temp, val);
		e->setText(wtemp);
		va_end(val);
	}
	else
		e->setText(L"");
}

void GUIManager::setText(int id, const wchar_t* txt, ...)
{
	wchar_t temp[MAX_TEXT_SIZE+1];
	irr::gui::IGUIElement *e=getElement(id);
	if(txt)
	{
		va_list val;
		va_start( val, txt );
		vswprintf(temp, MAX_TEXT_SIZE, txt, val);
		e->setText(temp);
		va_end(val);
	}
	else
		e->setText(L"");
}

void GUIManager::appendText(int id, const char* txt, ...)
{
	wchar_t temp[MAX_TEXT_SIZE+1];
	wchar_t wtemp[MAX_TEXT_SIZE+1];

	IGUIElement *e = getElement(id);

	if(e->getType() != EGUIET_LIST_BOX)
	{
		TRACE_WARN("tried to use appendText on a non-listbox element");
		return;
	}

	IGUIListBox *l = static_cast<IGUIListBox*>(e);

	if(l)
	{
		if(txt)
		{
			mbstowcs(temp, txt, MAX_TEXT_SIZE);

			va_list val;
			va_start( val, txt );
			vswprintf(wtemp, MAX_TEXT_SIZE, temp, val);
			va_end(val);

			l->addItem(wtemp);
		}
		l->setSelected(l->getItemCount()-1);
		l->setSelected(-1);
	}
}

void GUIManager::moveToScreenCenter(int id)
{
	irr::gui::IGUIElement *e=getElement(id);
	if(e)
	{
		e->setVisible(true);

		irr::core::rect<irr::s32> eRect=e->getAbsolutePosition();
		//eRect.getWidth()
		irr::s32 x = eRect.LowerRightCorner.X - eRect.UpperLeftCorner.X;
		irr::s32 y = eRect.LowerRightCorner.Y - eRect.UpperLeftCorner.Y;

		eRect.UpperLeftCorner.X = screenCenter.X - x/2;
		eRect.UpperLeftCorner.Y = screenCenter.Y - y/2;
		eRect.LowerRightCorner.X = eRect.UpperLeftCorner.X + x;
		eRect.LowerRightCorner.Y = eRect.UpperLeftCorner.Y + y;

		e->setRelativePosition(eRect);
	}
}

const char* GUIManager::getText(int id) const
{
	static char temp[MAX_TEXT_SIZE+1]="";
	irr::gui::IGUIElement *e=getElement(id);
	if(e)
		wcstombs(temp, e->getText(), MAX_TEXT_SIZE);
	return temp;
}
